﻿using Cake_Management.Models;
using Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Cake_Management.Controllers
{
    public class HT_CustomerController : Controller
    {
        // GET: HT_Customer
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult GetAll()
        {
            var result=new BLL.B_Customer().GetAll();
            var aa = from a in result
                     select new
                     {
                         a.Id,
                         a.CustomerName,
                         a.Password,
                         a.Email,
                         a.CName,
                         a.Areas,
                         a.QQ,
                         Avatar = $"<a href='{ConfigurationManager.AppSettings["ServerPath"] + a.Avatar}'target='_blank' onmouseover='viewIn(this)'onmouseout='viewOut()'>头像</a>",
                         Gender = GetLevel(a.Gender.Value),
                         a.Birthday,
                         cc = $"<div data-id='{a.Id}' class='btn btn-warning' data-edit onclick='Edit(this)'>编辑</div>  <div data-id='{a.Id}'  class='btn btn-danger'data-del onclick='Del(this)'>删除</div>"
                     };
            var data = new { data = aa };
            return Json(data,JsonRequestBehavior.AllowGet);
        }

        private string GetLevel(int level)
        {
            string result = "";
            switch (level)
            {
                case 3:
                    result = "隐藏";
                    break;
                case 2:
                    result = "女";
                    break;
                default:
                    result = "男";
                    break;
            }
            return result;
        }

        [HttpGet]
        public ActionResult Add()
        {
            
            return PartialView();
        }

        [HttpPost]
        public ActionResult Add(Customer customer)
        {
            var result = new
            {
                Base64 = customer.Avatar,
                Past = "Customer"
            };
            var aa = HttpHelper.HttpPost<string>(ConfigurationManager.AppSettings["ServerPath"]+ "api/Base64/Base64Code","post",result);
            customer.Avatar = aa.Data;
            new BLL.B_Customer().Add(customer);
            return Json(new { Code = 200, Message = "添加成功"});
        }


        [HttpPost]
        public ActionResult Delete(int id)
        {
            new BLL.B_Customer().Delete(id);
            return Json(new { Code = 200, Message = "删除成功" });
        }


        [HttpGet]
        public ActionResult Edit(int id)
        {
            return PartialView(new BLL.B_Customer().FindById(id));
        }

        [HttpPost]
        public ActionResult Edit(Customer customer)
        {
            if (customer.Avatar.Contains("，"))
            {
                var result = new
                {
                    Base64 = customer.Avatar,
                    Past = "Customer"
                };
                var aa = HttpHelper.HttpPost<string>(ConfigurationManager.AppSettings["ServerPath"] + "api/Base64/Base64Code", "post", result);
                customer.Avatar = aa.Data;
                new BLL.B_Customer().Add(customer);
                return Json(new { Code = 200, Message = "修改成功" });
            }
            else
            {
                new BLL.B_Customer().Updata(customer.Id, customer);
                return Json(new { Code = 200, Message = "修改成功" });
            }
            
        }
    }
}